﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

namespace LevelEditor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "All Files|*.*|Xml Files|*.xml";
            dlg.FilterIndex = 2;
            dlg.DefaultExt = "xml";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                XElement xRoot = new XElement("Level");
                XAttribute xTexture = new XAttribute("Texture", tbBrowse.Text);
                xRoot.Add(xTexture);
                XAttribute xLength = new XAttribute("Length", nudLength.Value);
                xRoot.Add(xLength);
                XAttribute xDifficulty = new XAttribute("Difficulty", nudDifficulty.Value);
                xRoot.Add(xDifficulty);
                XAttribute xChance;
                if( rb25.Checked )
                    xChance = new XAttribute("Chance", 0);
                else if (rb50.Checked)
                    xChance = new XAttribute("Chance", 1);
                else
                    xChance = new XAttribute("Chance", 2);
                xRoot.Add(xChance);

                XAttribute xSpeedupSpawn = new XAttribute("SpeedUpSpawn", nudSpawnRate.Value);
                xRoot.Add(xSpeedupSpawn);

                XAttribute xSpeedUpNum = new XAttribute("SpeedUpNum", nudCatch.Value);
                xRoot.Add(xSpeedUpNum);

                xRoot.Save(dlg.FileName);
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "All Files|*.*|Xml Files|*.xml";
            dlg.FilterIndex = 2;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                XElement xRoot = XElement.Load(dlg.FileName);

                XAttribute xTexture = xRoot.Attribute("Texture");
                XAttribute xLength = xRoot.Attribute("Length");
                XAttribute xDifficulty = xRoot.Attribute("Difficulty");
                XAttribute xChance = xRoot.Attribute("Chance");
                XAttribute xSpeedupSpawn = xRoot.Attribute("SpeedUpSpawn");
                XAttribute xSpeedUpNum = xRoot.Attribute("SpeedUpNum");

                tbBrowse.Text = xTexture.Value;
                nudLength.Value = decimal.Parse(xLength.Value);
                nudDifficulty.Value = decimal.Parse(xDifficulty.Value);
                if (int.Parse(xChance.Value) == 0)
                    rb25.Checked = true;
                else if (int.Parse(xChance.Value) == 1)
                    rb50.Checked = true;
                else
                    rb100.Checked = true;

                nudSpawnRate.Value = decimal.Parse(xSpeedupSpawn.Value);
                nudCatch.Value = decimal.Parse(xSpeedUpNum.Value);
            }
        }

        private void bBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if (DialogResult.OK == dlg.ShowDialog())
            {
                tbBrowse.Text = dlg.SafeFileName.Remove(dlg.SafeFileName.Count()-4, 4);
            }
        }
    }
}
